<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XYMTOP
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'tb:tbOrderAttachment:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsTbTbOrderAttachmentForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { TbOrderAttachment, tbOrderAttachmentSave, tbOrderAttachmentForm } from '/@/api/tb/tbOrderAttachment';
  import { officeTreeData } from '/@/api/sys/office';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('tb.tbOrderAttachment');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<TbOrderAttachment>({} as TbOrderAttachment);
  const getTitle = computed(() => ({
    icon: meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增tb_order_attachment') : t('编辑tb_order_attachment'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('附件所属状态id'),
      field: 'statusId',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('文件获取地址'),
      field: 'url',
      component: 'Input',
      componentProps: {
        maxlength: 1024,
      },
    },
    {
      label: t('文件创建时间'),
      field: 'createTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('文件最后更新时间'),
      field: 'updateTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('文件上传者用户id'),
      field: 'uploaderId',
      component: 'TreeSelect',
      componentProps: {
        api: officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '' },
        canSelectParent: false,
        allowClear: true,
      },
    },
    {
      label: t('文件类型'),
      field: 'fileType',
      component: 'Select',
      componentProps: {
        dictType: 'order_file_type',
        allowClear: false,
      },
    },
    {
      label: t('文件后缀名'),
      field: 'suffix',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('文件名'),
      field: 'fileName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
    },
    {
      label: t('备注'),
      field: 'comment',
      component: 'Input',
      componentProps: {
        maxlength: 1024,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await tbOrderAttachmentForm(data);
    record.value = (res.tbOrderAttachment || {}) as TbOrderAttachment;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        attachmentId: record.value.attachmentId,
        deleted: record.value.deleted,
      };
      // console.log('submit', params, data, record);
      const res = await tbOrderAttachmentSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
